
ITEM.base = "base_weapon"
ITEM.class = "wood"
ITEM.holdable = true
ITEM.label = "Wood"
ITEM.description = "Lumber. Maybe timber."
ITEM.weight = 3
ITEM.model = "models/props_debris/wood_board02a.mdl"
ITEM.icon = "wintersurvival/icon_wood"


ITEM.holdtype = "melee"
ITEM.viewmodel = {
	{
		bone = "ValveBiped.Bip01_R_Hand",
		model = "models/props_debris/wood_board02a.mdl",
		size = Vector(.5,.5,.5),
		pos = Vector(3,-1.5,-12),
		ang = Angle(0,0,0),
	},
}
ITEM.damage = 20
ITEM.range = 60 //Inches, look at player from thirdperson to find, then do a trace
ITEM.cooldown = .6

//Weapon

function ITEM:onPrimary(pl)
	local GM = gmod.GetGamemode()
	local itemtab,inv = self:getDatTab(pl)
	
	if CLIENT or not itemtab then
		return
	end
	
	if (itemtab.lastused + self.cooldown) < CurTime() then
		local trace = {}
		trace.start = pl:GetShootPos()
		trace.endpos = pl:GetShootPos() + (pl:GetAimVector() * self.range)
		trace.filter = pl
		local tr = util.TraceLine(trace)
		
		//Play animation
		pl:RestartGesture(pl:Weapon_TranslateActivity(ACT_HL2MP_GESTURE_RANGE_ATTACK))
		
		if tr.Hit then
			if tr.HitNonWorld and IsValid(tr.Entity) then
				local ent = tr.Entity
				local class = ent:GetClass()
				//pl:ChatPrint(ent:GetModel())
				
				//We may have to make sounds emit serverside only
				if SERVER and GM.IsDamageableClass(class) then
					ent:TakeDamage(self.damage,pl)
					
					//Play hit player sound
					pl:EmitSound(Sound("physics/flesh/flesh_impact_hard"..math.random(1,6)..".wav"),100,math.random(90,110))
					
				elseif GM.IsTreeModel(ent:GetModel()) then
					//Play hit tree sound
					pl:EmitSound(Sound("physics/concrete/rock_impact_hard"..math.random(1,6)..".wav"),100,math.random(90,110))
				else
					//Play hit world sound
					pl:EmitSound(Sound("physics/surfaces/sand_impact_bullet"..math.random(1,4)..".wav"),100,math.random(90,110))
				end
			else
				//Play hit world sound
				pl:EmitSound(Sound("physics/surfaces/sand_impact_bullet"..math.random(1,4)..".wav"),100,math.random(90,110))
			end
		else
			//Play swing sound
			pl:EmitSound(Sound("weapons/iceaxe/iceaxe_swing1.wav"),100,math.random(90,110))
		end
		itemtab.lastused = CurTime()
	else
		//We do not call the baseclass's function from the non-respective function (This is onPrimary, not FireAfterCooldown)
		self:FireAfterCooldown(pl)
	end
end

function ITEM:onSecondary(pl)
	if CLIENT then
		return
	end
	local GM = gmod.GetGamemode()
	local itemtab,inv = self:getDatTab(pl)
	
	if not itemtab then
		return
	end
	
	if (itemtab.lastused + self.cooldown) < CurTime() then
		local ent = ents.Create("ws_item")
		ent:SetPos(pl:GetShootPos())
		ent:SetAngles(pl:EyeAngles())
		//ent:SetAngles(Angle(math.random(-180,180),math.random(-180,180),math.random(-180,180))) //Backspin doesn't work with a random initial angle :(
		ent:GetTable().class = self.class
		ent:SetModel(self.model)
		ent:SetOwner(pl)
		timer.Simple(1,function()
			if IsValid(ent) then
				ent:SetOwner()
			end
		end)
		ent:Spawn()
		
		local phys = ent:GetPhysicsObject()
		phys:ApplyForceCenter(pl:GetAimVector() * math.pow(27,3))//:GetNormalized()
		
		//Apply -fore-spin and a slight roll for ultra-realism
		local angvel = Vector(0,math.random(1000,2000),0) //The positive z coordinate emulates the spin from a right-handed overhand throw
		angvel:Rotate(-1*ent:EyeAngles())
		angvel:Rotate(Angle(0,pl:EyeAngles().y,0))
		phys:AddAngleVelocity(angvel)
		//No two throws from the same starting point will land the same due to spin
		
		//Remove one rock
		pl:WSGive(self.class,-1)
		
		//Play animation
		pl:RestartGesture(pl:Weapon_TranslateActivity(ACT_HL2MP_GESTURE_RANGE_ATTACK))
		
		//Play swing sound
		pl:EmitSound(Sound("weapons/iceaxe/iceaxe_swing1.wav"),100,math.random(40,60))
		
		itemtab.lastused = CurTime()
	else
		self:FireSecondaryAfterCooldown(pl)
	end
end

//Miscellaneous

function ITEM:onPrecache()
	for i = 1,6 do
		Sound("physics/concrete/rock_impact_hard"..i..".wav")
		Sound("physics/flesh/flesh_impact_hard"..i..".wav")
	end
	for i = 1,4 do
		Sound("physics/surfaces/sand_impact_bullet"..i..".wav")
	end
	Sound("weapons/iceaxe/iceaxe_swing1.wav")
end